<template>

  <div>
    <div class="filter-container">
      <div class="filter-item">
        <span>附件名称:</span>
        <el-input v-model="listQuery.fileName" clearable placeholder="请输入附件名称" @keyup.enter="handleFilter" />
      </div>
      <el-button class="filter-item" type="primary" icon="Search" @click="handleFilter">搜索</el-button>
      <el-button class="filter-item" text type="primary" icon="Delete" @click="clearQuery">清除条件</el-button>
    </div>
    <div class="operationList">
      <div>
        <el-button class="filter-item" text type="primary" icon="Delete" @click="handleBatchDelete">批量删除</el-button>
      </div>
      <div>
        <el-button text type="primary" icon="RefreshRight" @click="handleFilter">刷新</el-button>
      </div>
    </div>
    <el-table ref="multipleTable" v-loading="listLoading" :row-class-name="tableRowClass" :data="list" fit border highlight-current-row tyle="width: 100%" header-cell-class-name="header-cell" @selection-change="handleSelectionChange">
      <el-table-column type="selection" />
      <el-table-column label="序号" width="100" type="index" :index="calIndex"/>
      <el-table-column min-width="200" label="文件名称">
        <template #default="scope">
          <span>{{ scope.row.fileName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="文件路径">
        <template #default="scope">
          <span><a :href="scope.row.filePath" target="_blank">{{ scope.row.filePath }}</a></span>
        </template>
      </el-table-column>
      <el-table-column min-width="90" label="文件大小">
        <template #default="scope">
          <span>{{ scope.row.fileSize }} K</span>
        </template>
      </el-table-column>
      <el-table-column min-width="80" label="扩展名">
        <template #default="scope">
          <span>{{ scope.row.fileExtension }}</span>
        </template>
      </el-table-column>
      <el-table-column min-width="160" label="上传时间">
        <template #default="scope">
          <span>{{ parseTime(scope.row.uploadTime, '{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column min-width="120" label="上传的IP">
        <template #default="scope">
          <span>{{ scope.row.uploadIp }}</span>
        </template>
      </el-table-column>

      <el-table-column label="操作" width="80">
        <template #default="scope">
          <el-button size="small" type="danger" plain @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" v-model:page="listQuery.page" v-model:limit="listQuery.limit" :total="total" :page-sizes="pageArray" @pagination="getList" />

  </div>
</template>

<script>
import { fetchAttachmentList, deleteAttachment } from '@/api/oss/attachment'
import Pagination from '@/components/Pagination/index.vue'
export default {
  name: 'AttachmentList',
  components: { Pagination },
  data() {
    return {
      list: null,
      total: 0,
      listLoading: true,
      pageArray: this.$store.dictStore.pageArray,
      listQuery: {
        page: 1,
        limit: this.$store.dictStore.defaultPageSize,
        importance: undefined,
        title: undefined,
        type: undefined
      },
      showReviewer: false,
      batchDeleteLoading: false,
      multipleSelection: []
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /**
     * 获取列表
     */
    getList(loading = true) {
      this.listLoading = loading
      fetchAttachmentList(this.listQuery).then(response => {
        this.list = response.data.data
        this.total = response.data.total
        this.listLoading = false
      })
    },
    /**
     * 搜索
     */
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    /**
     * 修改状态
     * @param row 当前行数据
     * @param status 状态
     */
    handleModifyStatus(row, status) {
      this.$message({
        message: '操作成功',
        type: 'success'
      })
      row.status = status
    },
    /**
     * 重置表单
     */
    resetTemp() {
      this.temp = {
        phone: '',
        code: '',
        data: ''
      }
    },
    /**
     * 勾选列表数据
     * @param val 选中行数据
     */
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    /**
     * 删除
     * @param row 行数据
     */
    handleDelete(row) {
      this.$confirm('确定删除该数据吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteAttachment([row.id]).then(response => {
          if (response.data.success) {
            row.$delAnim = true
            this.getList()
            this.$message.success(response.data.msg)
          } else {
            this.$message.error(response.data.msg)
          }
        })
      })
    },
    /**
     * 批量删除
     */
    handleBatchDelete() {
      if (this.multipleSelection.length) {
        this.$confirm('确定删除该数据吗?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          const ids = this.multipleSelection.map(item => item.id)
          this.batchDeleteLoading = true
          deleteAttachment(ids).then(response => {
            this.batchDeleteLoading = false
            if (response.data.success) {
              this.multipleSelection.forEach(item => {
                item.$delAnim = true
              })
              this.$refs.multipleTable.clearSelection()
              this.$message.success(response.data.msg)
              this.getList(false)
            } else {
              this.$message.error(response.data.msg)
            }
          }).catch(() => {
            this.batchDeleteLoading = false
          })
        })
      } else {
        this.$message({
          message: '至少选择一条日志',
          type: 'warning'
        })
      }
    }

  }
}
</script>
